extends layout

block headContent
	title Admin

block content
	h1.h3 Admin
	hr

	ul.nav.nav-tabs.mb-3
		li.nav-item
			a.nav-link.active(data-toggle="tab", href="#tab-details", role="tab") Details
		li.nav-item
			a.nav-link(data-toggle="tab", href="#tab-json", role="tab") JSON

	div.tab-content
		div.tab-pane.active(id="tab-details", role="tabpanel")
			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 Basics
					hr

					div.clearfix
						div.row
							div.summary-table-label Uptime
							div.summary-table-content.text-monospace
								- var uptime = moment.duration(new Date().getTime() - appStartTime);
								span #{uptime.format()}

			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 App Config
					hr

					pre
						code.json #{JSON.stringify(appConfig, null, 4)}

			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 Memory Stats
					hr

					div.table-responsive
						table.table.table-hover
							thead
								tr
									th.text-right Heap Size
									th.text-right Used Heap
									th.text-right Heap Limit
									th.text-right Physical Size
									th.text-right Available Size

							tbody
								tr.text-right.text-monospace
									td
										- var data = utils.formatLargeNumber(memstats.total_heap_size, 2);
										span #{data[0]}
											small  #{data[1].abbreviation}B

									td
										- var data = utils.formatLargeNumber(memstats.used_heap_size, 2);
										span #{data[0]}
											small  #{data[1].abbreviation}B

									td
										- var data = utils.formatLargeNumber(memstats.heap_size_limit, 2);
										span #{data[0]}
											small  #{data[1].abbreviation}B

									td
										- var data = utils.formatLargeNumber(memstats.total_physical_size, 2);
										span #{data[0]}
											small  #{data[1].abbreviation}B

									td
										- var data = utils.formatLargeNumber(memstats.total_available_size, 2);
										span #{data[0]}
											small  #{data[1].abbreviation}B

					

			if (JSON.stringify(cacheStats) != "{}")
				div.card.shadow-sm.mb-3
					div.card-body
						h3.h6 Cache Stats
						hr

						div.table-responsive
							table.table.table-hover
								thead
									tr
										th Cache
										th.text-right Hit
										th.text-right Miss
										th.text-right Hit Rate

								tbody
									each item, itemName in cacheStats
										tr.text-monospace
											td #{itemName}
											td.text-right #{item.hit.toLocaleString()}
											td.text-right #{item.miss.toLocaleString()}
											td.text-right
												if (item.hit > 0 || item.miss > 0)
													span #{(100 * item.hit / (item.hit + item.miss)).toLocaleString()}
														small  %
												else
													span -

			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 RPC Stats
					hr

					div.table-responsive
						table.table.table-hover.table-striped
							thead
								tr
									th Method
									th.text-right Count
									th.text-right Time
										small  (s)
									th.text-right Avg Time
										small  (ms)
									th.text-right Successes / Failures
									th.text-right Success Rate

							tbody
								each item, itemName in rpcStats
									tr.text-monospace
										td #{itemName}
										td.text-right #{item.count.toLocaleString()}
										td.text-right #{(item.time / 1000).toLocaleString()}
										td.text-right #{(item.time / item.count).toLocaleString()}
										td.text-right
											span.text-success #{item.successes.toLocaleString()}
											span.mx-1 /
											span.text-danger #{item.failures.toLocaleString()}

										td.text-right
											span #{new Decimal(item.successes).dividedBy(new Decimal(item.successes + item.failures)).times(100).toDP(1)}
												small  %

			if (electrumStats)
				div.card.shadow-sm.mb-3
					div.card-body
						h3.h6 Electrum Stats
						hr

						div.table-responsive
							table.table.table-hover.table-striped
								thead
									tr
										th Action
										th.text-right Count
										th.text-right First Seen
										th.text-right Last Seen

								tbody
									each item, itemName in electrumStats.base
										tr.text-monospace
											td #{itemName}
											td.text-right #{item.count.toLocaleString()}

											if (item.firstSeenAt)
												td.text-right #{moment.duration(new Date().getTime() - item.firstSeenAt.getTime()).format()} ago
											else
												td.text-right -

											if (item.lastSeenAt)
												td.text-right #{moment.duration(new Date().getTime() - item.lastSeenAt.getTime()).format()} ago
											else
												td.text-right -

						hr

						div.table-responsive
							table.table.table-hover.table-striped
								thead
									tr
										th RPC
										th.text-right Count
										th.text-right Time
											small  (s)
										th.text-right Avg Time
											small  (ms)
										th.text-right Successes / Failures
										th.text-right Success Rate

								tbody
									each item, itemName in electrumStats.rpc
										tr.text-monospace
											td #{itemName}
											td.text-right #{item.count.toLocaleString()}
											td.text-right #{(item.time / 1000).toLocaleString()}
											td.text-right #{(item.time / item.count).toLocaleString()}
											td.text-right
												span.text-success #{item.successes.toLocaleString()}
												span.mx-1 /
												span.text-danger #{item.failures.toLocaleString()}

											td.text-right
												span #{new Decimal(item.successes).dividedBy(new Decimal(item.successes + item.failures)).times(100).toDP(1)}
													small  %

			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 Error Stats
					hr

					if (JSON.stringify(errorStats) == "{}")
						span.text-success No errors encountered

					else
						div.table-responsive
							table.table.table-hover.table-striped
								thead
									tr
										th Error ID
										th.text-right Count
										th.text-right First Seen
										th.text-right Last Seen

								tbody
									each item, itemName in errorStats
										tr.text-monospace
											td #{itemName}
											td.text-right #{item.count.toLocaleString()}
											td.text-right #{moment.duration(new Date().getTime() - item.firstSeen).format()} ago
											td.text-right #{moment.duration(new Date().getTime() - item.lastSeen).format()} ago
									

		div.tab-pane(id="tab-json", role="tabpanel")
			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 Memory Stats
					hr

					div.highlight
						pre
							code.json #{JSON.stringify(memstats, null, 4)}

			div.card.shadow-sm.mb-3
				div.card-body
					h3.h6 Error Stats
					hr

					div.highlight
						pre
							code.json #{JSON.stringify(errorStats, null, 4)}
